package net.ruangtedy.java.spring.formhandling.web.controller;

import net.ruangtedy.java.spring.formhandling.domain.Person;
import net.ruangtedy.java.spring.formhandling.domain.UserDetails;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/user")
public class ModelAttributeExampleController {
	
	@Autowired
	private UserDetails userDetails;
	
	@RequestMapping(method=RequestMethod.GET)
	public String UserPage() {
		return "user-details";
	}
	
    @RequestMapping(method = { RequestMethod.POST })
	public String getMethod(@ModelAttribute("userDetails") UserDetails userDetails){
		System.out.println("User Name : " + userDetails.getUserName());
		System.out.println("Email Id : " + userDetails.getEmailId());
		return "user-details";
	}

	//This method is invoked before the above method
//	@ModelAttribute()
//	public UserDetails getAccount(@RequestParam String user, @RequestParam String emailId){
//		System.out.println("User Value from Request Parameter : " + user);
//		userDetails.setUserName(user);
//		userDetails.setEmailId(emailId);
//		return userDetails;
//	}
    
	//This method is invoked before the above method
	@ModelAttribute
	public UserDetails getAccount(){
		System.out.println("User Name sebelum : " + userDetails.getUserName());
		System.out.println("Email Id sebelum : " + userDetails.getEmailId());
			return userDetails;
	}
	
}
